package com.biboheart.adapter.express.support;

import com.biboheart.adapter.express.actuator.GenericActuator;
import com.biboheart.adapter.support.utils.ObjectTypeUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

final class RoundActuator implements GenericActuator {
    @Override
    public Object execute(Object source, Object... args) {
        if (null == source) {
            return null;
        }
        Double number = ObjectTypeUtils.convertType(source, Double.class);
        int fractional = args.length > 0 ? ObjectTypeUtils.convertType(args[0], Integer.class) : 2;
        return BigDecimal.valueOf(number).setScale(fractional, RoundingMode.HALF_UP);
    }
}
